//
//  EJSAccountBillsViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2018/1/15.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "EJSAccountBillsViewController.h"
#import "EJSAccountBillTableViewCell.h"
#import "EJSBalanceLogRequest.h"

@interface EJSAccountBillsViewController ()<UITableViewDataSource>{
    NSInteger _currentPage;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation EJSAccountBillsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configOwnViews{
    self.title = @"账户历史";
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSAccountBillTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSAccountBillTableViewCell"];
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
}

- (void)loadData{
    _currentPage = 1;
    [self.tableView.mj_footer resetNoMoreData];
    [self getAccountBills];
}

- (void)loadMoreData{
    _currentPage ++;
    [self getAccountBills];
}

- (void)getAccountBills{
    EJSBalanceLogRequest *req = [EJSBalanceLogRequest new];
    req.page = @(_currentPage).stringValue;
    req.pageLimit = @"20";
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        EJSBalanceLogResponseModel *model = response.data;
        if(_currentPage == 1){
            [self.dataSource removeAllObjects];
        }
        [self.dataSource addObjectsFromArray:model.list];
        if(model.list.count < 20){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        
        if(self.dataSource.count > 0){
            [self hideNoDataView];
        }else{
            [self showNoDataViewWithText:@"暂无账户历史"];
        }
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}


#pragma mark TableViewDataSoruce
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EJSAccountBillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSAccountBillTableViewCell" forIndexPath:indexPath];
    [cell setBalanceLogModel:self.dataSource[indexPath.row]];
    return cell;
}


@end
